package cn.hyy.FVOnline.service.impl;

import cn.hyy.FVOnline.entity.*;
import cn.hyy.FVOnline.mapper.*;
import cn.hyy.FVOnline.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service("orderService")
public class OrderServiceImpl implements OrderService {

    @Autowired
    private OrderMapper orderMapper;

    @Override
    public Order createOrder(Order order) {
        Long createTime = System.currentTimeMillis();
        order.setOrderNumber(order.getuId().toString() + createTime.toString());
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date();
        date.setTime(createTime);
        order.setCreateTime(dateFormat.format(date));
        Long payTime = createTime + (1000 * 60);
        date.setTime(payTime);
        order.setPayTime(dateFormat.format(date));
        order.setState(1);
        orderMapper.createOrder(order);
        return orderMapper.findById(order.getId());
    }

    @Autowired
    private AddressMapper addressMapper;

    @Autowired
    private GoodsMapper goodsMapper;


    @Override
    public PageEntitys findByShopId(Integer shopid, Integer page, Integer pageSize) {

        Integer start = (page - 1) * pageSize;

        List<Order> orders = orderMapper.findByShopId(shopid, start, pageSize);

        for (Order order : orders) {
            Address address = addressMapper.findById(order.getAddressId());
            Goods goods = goodsMapper.findById(order.getGoodsId());
            order.setAddress(address);
            order.setGoods(goods);
        }

        PageEntitys pageEntitys = new PageEntitys();
        pageEntitys.setTotal(orderMapper.findShopTotal(shopid));
        pageEntitys.setCurrent(page);
        pageEntitys.setOrders(orders);

        return pageEntitys;
    }

    @Override
    public void sendExpress(Order order) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date();
        date.setTime(System.currentTimeMillis());
        order.setSendTime(dateFormat.format(date));
        order.setState(2);
        orderMapper.sendExpress(order);
        Order o = orderMapper.findById(order.getId());
        orderMapper.addMessage(o.getOrderNumber(),o.getuId());
    }

    @Override
    public PageEntitys findByShopIdTwo(Integer shopid, Integer page, Integer pageSize) {
        Integer start = (page - 1) * pageSize;

        List<Order> orders = orderMapper.findByShopIdTwo(shopid, start, pageSize);

        for (Order order : orders) {
            Address address = addressMapper.findById(order.getAddressId());
            Goods goods = goodsMapper.findById(order.getGoodsId());
            order.setAddress(address);
            order.setGoods(goods);
        }

        PageEntitys pageEntitys = new PageEntitys();
        pageEntitys.setTotal(orderMapper.findShopTotalTwo(shopid));
        pageEntitys.setCurrent(page);
        pageEntitys.setOrders(orders);

        return pageEntitys;
    }

    @Autowired
    private ShopMapper shopMapper;


    @Override
    public PageEntitys findByUserId(Integer userid, Integer page, Integer pageSize) {
        Integer start = (page - 1) * pageSize;
        List<Order> orders = orderMapper.findUserOrder(userid,start,pageSize);
        for (Order order : orders) {
            Goods goods = goodsMapper.findById(order.getGoodsId());
            Shop shop = shopMapper.findById(order.getShopId());
            order.setGoods(goods);
            order.setShop(shop);
        }
        PageEntitys pageEntitys = new PageEntitys();
        pageEntitys.setTotal(orderMapper.findUserTotal(userid));
        pageEntitys.setCurrent(page);
        pageEntitys.setOrders(orders);
        return pageEntitys;
    }

    @Override
    public void receive(Integer id) {
        orderMapper.receive(id);
    }

    @Autowired
    private UserMapper userMapper;

    @Override
    public Order findById(Integer id) {
        Order order = orderMapper.findByOrderId(id);
        Address address = addressMapper.findById(order.getAddressId());
        Goods goods = goodsMapper.findById(order.getGoodsId());
        Shop shop = shopMapper.findById(order.getShopId());
        User u = new User();
        u.setId(shop.getUserId());
        User user = userMapper.findById(u);
        order.setAddress(address);
        order.setGoods(goods);
        order.setUser(user);
        return order;
    }

    @Override
    public void evaluate(Integer id) {
        orderMapper.evaluate(id);
    }

    @Override
    public PageEntitys getOrders(String what, Integer page, Integer pageSize) {
        if (what == null || what.length() == 0){
            what = "%" + what + "%";
        }
        Integer start = (page - 1) * pageSize;
        List<Order> orders = orderMapper.getOrder(what,start,pageSize);
        PageEntitys pageEntitys = new PageEntitys();
        pageEntitys.setTotal(orderMapper.getOrderTotal(what));
        pageEntitys.setCurrent(page);
        pageEntitys.setOrders(orders);
        return pageEntitys;
    }

    @Override
    public List<Message> getMessage(Integer uid) {
        return orderMapper.getMessage(uid);
    }

    @Override
    public void delMessage(Integer uid) {
        orderMapper.delMessage(uid);
    }
}
